package airthmetic.exercise.dp;

public class _122_买卖股票的最佳时机II {

    public static int maxProfit(int[] prices) {
        int n = prices.length;
        int[][] dp = new int[n][2];
        for(int i=0; i<n; i++){
            if(i == 0){
                dp[i][0] = 0;
                dp[i][1] = -prices[i];
                continue;
            }
            dp[i][0] = Math.max(dp[i-1][1] +prices[i], dp[i-1][0]);
            dp[i][1] = Math.max(dp[i-1][0]-prices[i] ,dp[i-1][1]);
        }
        return dp[n-1][0];
    }


    // 状态压缩
    public static int maxProfit2(int[] prices) {
        int n = prices.length;
        int i_0 = 0;
        int i_1 = Integer.MIN_VALUE;

        for(int i=0; i<n; i++){
            int temp = i_0;
            i_0 = Math.max(i_1 +prices[i], i_0);
            i_1 = Math.max(temp-prices[i] ,i_1);
        }
        return i_0;
    }

    public static void main(String[] args) {
        System.out.println(maxProfit2(new int[]{7,1,5,3,6,4}));
    }
}
